// pages/dh/dh.js
const db = wx.cloud.database()
const cont = db.collection('Order')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: '',
    Goods:[],
    Orders:[],
    show:false,
    radioChange:'未选',
    DelectId:''
  },
  //获取当前时间
  getNowDatTime:function(){
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate<=9) {
      strDate = "0" + strDate;
    }
    var time = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
    console.log(time);
    return time;
  },
  ConfirmReceipt:function(e){
    var that=this
    wx.showModal({
      title: '确认收货',
      content: '是否确认付款',
      success: function (res) {
        if (res.confirm) {
          // wx.showLoading({
          //   title: '加载中',
          // })
          db.collection('Order').doc(e.currentTarget.dataset.id).update({
            data: {
              // 表示指示数据库将字段自增 10
              state: '待评价',
              ReceivingTime:that.getNowDatTime()
            },
            success: function(res) {
              if(that.data.currentTab=='全部'){
                that.getOrder2(that.data.currentTab)
              }else{
                that.getOrder(that.data.currentTab)
              }
              console.log(res.data)
            }
          })
          // wx.hideLoading();
        } else {
          console.log(res.confirm)
        }
      }
    })
  },
  ckwl:function(e){
    console.log(this.data.Orders[e.currentTarget.dataset.id].CourierNumber)
    console.log(this.data.Orders[e.currentTarget.dataset.id].id)
    var OrderId=this.data.Orders[e.currentTarget.dataset.id].id
    var CourierNumber=this.data.Orders[e.currentTarget.dataset.id].CourierNumber
    wx.navigateTo({
      url: "/pages/express/express?OrderId=" + OrderId +"&CourierNumber="+CourierNumber,
    })
  },
  tz:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/pages/OrderDetails/OrderDetails?id=" + e.currentTarget.dataset.id +"",
    })
  },
  Evaluation:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/pages/Evaluate/Evaluate?id=" + e.currentTarget.dataset.id +"",
    })
  },
  ViewEvaluation:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/pages/ViewEvaluation/ViewEvaluation?OrderId=" + e.currentTarget.dataset.id +"",
    })
  },
  fh:function(){
    wx.navigateBack({
      delta: 1
    })
  },
// 点击tab切换
swichNav: function (e) {
  var that = this;
  if (this.data.currentTab === e.target.dataset.current) {
    return false;
  } else {
    if(e.target.dataset.current==''){
      that.setData({
        currentTab: ''
      })
      this.getOrder2()
    }else{
      that.setData({
        currentTab: e.target.dataset.current
      })
      this.getOrder(e.target.dataset.current)
    }
    
    
  }
},
radioChange: function (e) {
  this.setData({
    radioChange:e.detail.value
  })
  console.log('radio发生change事件，携带value值为：', e.detail.value)
},
qx:function(){
  var that=this
  if(this.data.radioChange=='已选'){
    this.show()
    db.collection('Order').doc(this.data.DelectId).remove({
      success: function(res) {
        console.log(res.data)
        if(that.data.currentTab=='全部'){
          that.getOrder2(that.data.currentTab)
        }else{
          that.getOrder(that.data.currentTab)
        }
      }
    })
  }else{
    wx.showToast({
      title: "请选择取消订单的原因",
      icon: "none"
    })
  }
},
show:function(){
  this.setData({
    show:!this.data.show
  })
},
show2:function(e){
  console.log(e.currentTarget.dataset.id)
  this.setData({
    show:!this.data.show,
    DelectId:e.currentTarget.dataset.id
  })
},
fk:function(e){
  console.log(e.currentTarget.dataset.id)
  var that=this
  wx.showModal({
    title: '付款',
    content: '是否进行支付',
    success: function (res) {
      if (res.confirm) {
        // wx.showLoading({
        //   title: '加载中',
        // })
        db.collection('Order').doc(e.currentTarget.dataset.id).update({
          data: {
            // 表示指示数据库将字段自增 10
            state: '待发货',
            PaymentTime:that.getNowDatTime()
          },
          success: function(res) {
            console.log(res.data)
            if(that.data.currentTab=='全部'){
              that.getOrder2(that.data.currentTab)
            }else{
              that.getOrder(that.data.currentTab)
            }
            // wx.hideLoading();
          }
        })
      } else {
        console.log(res.confirm)
      }
    }
  })
},
getOrder:function(state){
  var that=this
  wx.showLoading({
    title: '加载中',
  })
  that.setData({
    Orders:[]
  })
  wx.cloud.callFunction({
    name: "login"
  }).then(res => {
    console.log(res.result.openid);
    cont.where({
      state:state,
      _openid:res.result.openid
    }).get()
      .then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
        for(let i=0; i < res.data.length;i++){
          var id = "Orders[" + i + "].id"
          var Consignee = "Orders[" + i + "].Consignee"
          var DeliveryTime = "Orders[" + i + "].DeliveryTime"
          var GoodsId = "Orders[" + i + "].GoodsId"
          var ReceivingTime = "Orders[" + i + "].ReceivingTime"
          var Remarks = "Orders[" + i + "].Remarks"
          var SellOpenid = "Orders[" + i + "].SellOpenid"
          var Telephone = "Orders[" + i + "].Telephone"
          var openid = "Orders[" + i + "].openid"
          var address = "Orders[" + i + "].address"
          var color = "Orders[" + i + "].color"
          var number = "Orders[" + i + "].number"
          var rmb = "Orders[" + i + "].rmb"
          var rmb2 = "Orders[" + i + "].rmb2"
          var size = "Orders[" + i + "].size"
          var state = "Orders[" + i + "].state"
          var GoodsTitle = "Orders[" + i + "].GoodsTitle"
          var SellNickName = "Orders[" + i + "].SellNickName"
          var SellAvatarUrl = "Orders[" + i + "].SellAvatarUrl"
          var GoodsImg = "Orders[" + i + "].GoodsImg"
          var express = "Orders[" + i + "].express"
          var CourierNumber = "Orders[" + i + "].CourierNumber"
          that.setData({
            [id] :res.data[i]._id,
            [Consignee]:res.data[i].Consignee,
            [DeliveryTime]:res.data[i].DeliveryTime,
            [GoodsId]:res.data[i].GoodsId,
            [ReceivingTime]:res.data[i].ReceivingTime,
            [Remarks]:res.data[i].Remarks,
            [SellOpenid]:res.data[i].SellOpenid,
            [Telephone]:res.data[i].Telephone,
            [openid]:res.data[i]._openid,
            [address]:res.data[i].address,
            [color]:res.data[i].color,
            [number]:res.data[i].number,
            [rmb]:(res.data[i].rmb).toFixed(2),
            [rmb2]:(res.data[i].rmb*res.data[i].number+res.data[i].express).toFixed(2),
            [size]:res.data[i].size,
            [state]:res.data[i].state,
            [GoodsTitle]:res.data[i].GoodsTitle,
            [SellNickName]:res.data[i].SellNickName,
            [SellAvatarUrl]:res.data[i].SellAvatarUrl,
            [GoodsImg]:res.data[i].GoodsImg,
            [express]:res.data[i].express,
            [CourierNumber]:res.data[i].CourierNumber,
          })

        }
       console.log( this.data.Orders)
       wx.hideLoading();
      })
      .catch(err => {
        wx.hideLoading();
        console.error(err)
    })

  }).catch(err => {
    console.log(err);
  })
},
getOrder2:function(){
  var that=this
  wx.showLoading({
    title: '加载中',
  })
  that.setData({
    Orders:[]
  })
  wx.cloud.callFunction({
    name: "login"
  }).then(res => {
    console.log(res.result.openid);
    cont.where({
      _openid:res.result.openid
    }).get()
      .then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
        for(let i=0; i < res.data.length;i++){
          var id = "Orders[" + i + "].id"
          var Consignee = "Orders[" + i + "].Consignee"
          var DeliveryTime = "Orders[" + i + "].DeliveryTime"
          var GoodsId = "Orders[" + i + "].GoodsId"
          var ReceivingTime = "Orders[" + i + "].ReceivingTime"
          var Remarks = "Orders[" + i + "].Remarks"
          var SellOpenid = "Orders[" + i + "].SellOpenid"
          var Telephone = "Orders[" + i + "].Telephone"
          var openid = "Orders[" + i + "].openid"
          var address = "Orders[" + i + "].address"
          var color = "Orders[" + i + "].color"
          var number = "Orders[" + i + "].number"
          var rmb = "Orders[" + i + "].rmb"
          var rmb2 = "Orders[" + i + "].rmb2"
          var size = "Orders[" + i + "].size"
          var state = "Orders[" + i + "].state"
          var GoodsTitle = "Orders[" + i + "].GoodsTitle"
          var SellNickName = "Orders[" + i + "].SellNickName"
          var SellAvatarUrl = "Orders[" + i + "].SellAvatarUrl"
          var GoodsImg = "Orders[" + i + "].GoodsImg"
          var express = "Orders[" + i + "].express"
          var CourierNumber = "Orders[" + i + "].CourierNumber"
          that.setData({
            [id] :res.data[i]._id,
            [Consignee]:res.data[i].Consignee,
            [DeliveryTime]:res.data[i].DeliveryTime,
            [GoodsId]:res.data[i].GoodsId,
            [ReceivingTime]:res.data[i].ReceivingTime,
            [Remarks]:res.data[i].Remarks,
            [SellOpenid]:res.data[i].SellOpenid,
            [Telephone]:res.data[i].Telephone,
            [openid]:res.data[i]._openid,
            [address]:res.data[i].address,
            [color]:res.data[i].color,
            [number]:res.data[i].number,
            [rmb]:res.data[i].rmb.toFixed(2),
            [rmb2]:(res.data[i].rmb*res.data[i].number+res.data[i].express).toFixed(2),
            [size]:res.data[i].size,
            [state]:res.data[i].state,
            [GoodsTitle]:res.data[i].GoodsTitle,
            [SellNickName]:res.data[i].SellNickName,
            [SellAvatarUrl]:res.data[i].SellAvatarUrl,
            [GoodsImg]:res.data[i].GoodsImg,
            [express]:res.data[i].express,
            [CourierNumber]:res.data[i].CourierNumber,
          })

        }
       console.log( this.data.Orders)
       wx.hideLoading();
      })
      .catch(err => {
        console.error(err)
        wx.hideLoading();
    })

  }).catch(err => {
    console.log(err);
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.order)
    this.setData({
      currentTab:options.order
    })
    this.getOrder(options.order)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})